package com.feitubc.tools.fileimport.builder;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.feitubc.tools.fileimport.domain.FileStrategySetting;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 说明：Safe predefined FileStrategyBuilders container.
 * @author zhc
 */
public class FileStrategyBuilders {
	
	private final List<FileStrategySetting> fileStrategySettings;

	private final Map<String, Map<String, FileStrategySetting>> fileStrategyBuilders;
	
	public FileStrategyBuilders(List<FileStrategySetting> fileStrategySettings) {
        this.fileStrategySettings = Collections.unmodifiableList(fileStrategySettings);
        if (fileStrategySettings == null || fileStrategySettings.isEmpty()) {
			throw new NullPointerException("fileStrategySettings is empty");
		}
        Map<String, Map<String, FileStrategySetting>> builders = Maps.newHashMap();
        for (int i = 0; i < this.fileStrategySettings.size(); i++) {
        	FileStrategySetting setting = this.fileStrategySettings.get(i);
			if (builders.containsKey(setting.getGroupType())) {
				builders.get(setting.getGroupType()).put(setting.getStrategyServiceType(), setting);
			} else {
				Map<String, FileStrategySetting> map = Maps.newHashMap();
				map.put(setting.getStrategyServiceType(), setting);
				builders.put(setting.getGroupType(), map);
			}
		}
		this.fileStrategyBuilders = Collections.unmodifiableMap(builders);
    }
	
	/**
     * fetch predefined fileStrategyBuilders by groupType.
     * 
     * @param groupType
     * @return
     * @throws NullPointerException
     *             no fileStrategySetting found.
     */
    public List<FileStrategySetting> getAll(String groupType) throws NullPointerException {
    	if (fileStrategyBuilders == null || !fileStrategyBuilders.containsKey(groupType)) {
    		throw new NullPointerException("No such predefined excelStrategys: " + groupType);
    	}
    	
    	Map<String, FileStrategySetting> map = this.fileStrategyBuilders.get(groupType);
    	
    	// 通过ArrayList构造函数把map.entrySet()转换成list
        List<FileStrategySetting> list = Lists.newArrayList(map.values());
        // 通过比较器实现比较排序
        Collections.sort(list, new Comparator<FileStrategySetting>() {
            public int compare(FileStrategySetting o1, FileStrategySetting o2) {
            	if (o1 != null && o1.getPriority() != null
            			&& o2 != null && o2.getPriority() != null) {
            		return o1.getPriority() - o2.getPriority();
            	}
            	return 0;
            }
        });
        
        return list;
    }
    
    /**
     * fetch predefined FileStrategySetting by groupType and strategyServiceType.
     * 
     * @param groupType
     * @param strategyServiceType
     * @return
     */
    public FileStrategySetting get(String groupType, String strategyServiceType) {
    	Map<String, FileStrategySetting> map = this.fileStrategyBuilders.get(groupType);
    	if (map != null && !map.isEmpty()) {
    		return map.get(strategyServiceType);
    	}
    	return null;
    }
}
